const jwt = require('jsonwebtoken')

const { PRIVATE_KEY } = require('../config/screct')

class loginController {
  sign(ctx, next) {
    // 1 获取用户信息
    const { id, name } = ctx.user
    // console.log(id,name)
    // console.log(privateKey)
    // const privateKey = fs.readFileSync('../config/keys/private.key'

    // 2颁发令牌
    const payload = { id, name }
    // console.log(payload)
    const token = jwt.sign(payload, PRIVATE_KEY, {
      expiresIn: 60 * 60 * 60,
      algorithm: 'RS256',
    })
    // console.log(token)
    // 3、返回用户信息
    ctx.body = {
      code: 0,
      data: {
        id,
        name,
        token,
      },
    }
  }
  test(ctx, next) {
    ctx.body = '验证身份通过'
  }
}

module.exports = new loginController()
